package com.qianniu.napi.admin.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;

import com.qianniu.napi.admin.entity.English;
import com.qianniu.napi.admin.entity.JsonDO;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.User;
import com.qianniu.napi.admin.service.IEnglishService;

import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.helper.HttpHelper;
import com.qianniu.napi.common.util.EncryptUtil;
import com.qianniu.napi.common.util.ObjectUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * <p>
 * 
 * </p>
 *
 * @author chenxuebin
 * @since 2019-01-28
 */
@Controller
public class EnglishController {

    private static final Logger logger = LoggerFactory.getLogger(EnglishController.class);

    @Autowired
    private IEnglishService englishService;

    @RequestMapping("admin/english/index")
    public String index() {
        return "admin/english";
    }

    @RequestMapping("/admin/english/list")
    @ResponseBody
    public Page<English> list(@RequestParam(value = "offset", required = false) Integer offset,
                                @RequestParam(value = "status", required = false) Integer status,
                                @RequestParam(value = "search", required = false) String search,
                                @RequestParam(value = "limit", required = false) Integer limit) {
        Wrapper<English> wrapper = new EntityWrapper<English>();
        wrapper.eq("mid", ObjectUtil.getMid());
//        if (!Strings.isNullOrEmpty(search))
//            wrapper.like("order_no", search).or().like("remark", search);

        wrapper.orderBy("id", false);
        Page<English> page = englishService.selectPage(new Page<English>(offset/limit+1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/english/add")
    public String add(@Valid English bean, BindingResult bindingResult) {
        // 字段校验
        if( bindingResult.hasErrors())
        {
            logger.debug( "There are errors! {}", bindingResult );
            return null;
        }
        bean.setCreatetime(new Date());
        bean.insert();
        return "admin/english";
    }

    @RequestMapping("/admin/english/getEnglishById")
    @ResponseBody
    public English getEnglishById(long id) {
        English english = englishService.selectById(id);
        return english;
    }

    @RequestMapping("/admin/english/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = englishService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/english/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        English bean = new English();
        bean.setId(id);
        englishService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/english/modify")
    public String update(English bean) {
        boolean isSuccc = englishService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }


}
